package com.taikang.hmp.hbusiness.diabetes.measure.meal.action.intface;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hbusiness.diabetes.measure.meal.service.IMealService;

/** 
 * 膳食
 * author:qrp
 * time:2015年8月5日 上午11:06:11
 */
public class MealAction extends BaseAction{
	private static Logger logger = LoggerFactory.getLogger(MealAction.class);
	
	
	private IMealService mealService;


	public IMealService getMealService() {
		return mealService;
	}

	public void setMealService(IMealService mealService) {
		this.mealService = mealService;
	}

	public String executeAction()  {
		if ("saveMeal".equals(requestDto.getMethod())) {
			return saveMeal();
		}else if("getMealList".equals(requestDto.getMethod())){
			return getMealList();
		}else if("getDayMealList".equals(requestDto.getMethod())){
			return getDayMealList();
		}else if("getDayStandardMealList".equals(requestDto.getMethod())){
			return getDayStandardMealList();
		}else {
			throw new TKException("no support method:"+requestDto.getMethod());
		}
	}
	
	/**
	 * 保存膳食测量值
	 * 
	 * @return
	 */
	public String saveMeal(){
		logger.info("进入saveMeal方法体:"+requestDto.toString());
		//通用校验
		ResponseDto responseDto = new ResponseDto();			
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		String	result = mealService.saveMeal(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("result", result);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		logger.info("saveMeal方法成功返回结果："+responseDto.toString());
		return SUCCESS;
	}

	/**
	 * 获取膳食列表
	 * 
	 * @return
	 */
	public String getMealList() {
		logger.info("进入getMealList方法体:"+requestDto.toString());
		//通用校验
		ResponseDto responseDto = new ResponseDto();			
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		Dto resultList = mealService.getMealList(paramDto);
		
		Dto result = (Dto)resultList.get("result");
		String date = "";
		String eattype_cd = "";
		if(result != null){
			date = result.getAsString("date");
			eattype_cd = result.getAsString("eattypecd");
		}
		//
		Dto dto = new BaseDto();
		dto.put("date", date);
		dto.put("eattypecd", eattype_cd);
		dto.put("calorie", "0");
		dto.put("resultlist", resultList.get("detailList"));//这个是详细膳食

		responseDto.pushResultDto(dto);
		//
		super.setDataMap(responseDto);
		logger.info("getMealList方法成功返回结果："+responseDto.toString());
		return SUCCESS;
	}

	/**
	 * 获取日膳食列表
	 */
	public String getDayMealList() {
		logger.info("进入getDayMealList方法体:"+requestDto.toString());
		//通用校验
		ResponseDto responseDto = new ResponseDto();		
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		//
		paramDto = super.appendRequestBase(paramDto, requestDto);
		Dto resultList = mealService.getDayMealList(paramDto);
		//Dto result = (Dto)resultList.get("result");//不知道为啥之前没用这个参数，那返回他干啥
		Dto retDto = new BaseDto();
		retDto.put("directmsg", "");
		retDto.put("calorie", "0");
		retDto.put("resultlist", resultList.get("detailList"));
		//返回餐次是否已完成
		Dto is_eat = mealService.getIsEat(paramDto);
		retDto.put("iscomplete", is_eat);
		responseDto.pushResultDto(retDto);

		super.setDataMap(responseDto);
		logger.info("getDayMealList方法成功返回结果："+responseDto.toString());
		return SUCCESS;
	}
	
	/**
	 * 获取日标准膳食列表
	 * 油、盐、奶类及奶制品、大豆类及坚果、畜禽肉类、鱼虾类、蛋类、蔬菜类、水果类、谷类薯及杂豆（依次代表0-9）
	 * @return
	 */
	public String getDayStandardMealList(){
		logger.info("进入获取日标准膳食列表getDayStandardMealList方法体:"+requestDto.toString());
		//通用校验
		ResponseDto responseDto = new ResponseDto();		
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		//
		paramDto = super.appendRequestBase(paramDto, requestDto);
		Dto resultList = mealService.getDayStandardMealList(paramDto);
		Dto result = (Dto)resultList.get("result");
		Dto retDto = new BaseDto();
		retDto.put("directmsg", result.getAsString("directmsg"));
		retDto.put("calorie", "0");
		retDto.put("resultlist", resultList.get("resultList"));
		responseDto.pushResultDto(retDto);
		//
		super.setDataMap(responseDto);
		logger.info("getDayStandardMealList方法成功返回结果："+responseDto.toString());
		return SUCCESS;
	}
}
